package com.core.cloudcommon.redis.service;

import com.core.cloudcommon.CommonCustomException;
import com.core.cloudcommon.redis.enums.CloudRedisTemplateResultCode;
import com.core.cloudcommon.redis.vo.CloudRedisVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author =====Lqj=====
 * @className CloudRedisTemplateServiceImpl
 * @description
 * @create 2024/11/17 20:37
 **/
@Slf4j
@Component
public class CloudRedisTemplateService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 指定缓存失效时间
     *
     * @return
     */
    public Boolean expire(CloudRedisVO cloudRedisVO) {
        if (Objects.isNull(cloudRedisVO)) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300003.getCode(), CloudRedisTemplateResultCode.CODE300003.getMsg());
        }
        if (null == cloudRedisVO.getKey()) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300001.getCode(), CloudRedisTemplateResultCode.CODE300001.getMsg());
        }
        try {
            redisTemplate.expire(cloudRedisVO.getKey(), cloudRedisVO.getTime(), TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 批量删除缓存
     */
    public void del(CloudRedisVO cloudRedisVO) {
        if (cloudRedisVO.getListKey().isEmpty() && cloudRedisVO.getKey() == null) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300001.getCode(), CloudRedisTemplateResultCode.CODE300001.getMsg());
        }
        redisTemplate.delete(cloudRedisVO.getListKey());
    }

    public void del(String key) {
        if (key == null) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300001.getCode(), CloudRedisTemplateResultCode.CODE300001.getMsg());
        }
        redisTemplate.delete(Arrays.asList(key));
    }


    /**
     * 普通缓存放入
     *
     * @return true成功 false失败
     */
    public Boolean set(CloudRedisVO cloudRedisVO) {
        if (null == cloudRedisVO.getKey()) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300001.getCode(), CloudRedisTemplateResultCode.CODE300001.getMsg());
        }
        if (null == cloudRedisVO.getValue()) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300002.getCode(), CloudRedisTemplateResultCode.CODE300002.getMsg());
        }
        try {
            if (null == cloudRedisVO.getTime()) {
                cloudRedisVO.setTime(7200l);
            }
            redisTemplate.opsForValue().set(cloudRedisVO.getKey(), cloudRedisVO.getValue(), cloudRedisVO.getTime(), TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    /**
     * 根据key 获取过期时间
     *
     * @return 时间(秒) 返回0代表为永久有效
     */
    public Long getExpire(CloudRedisVO cloudRedisVO) {
        if (null == cloudRedisVO.getKey()) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300001.getCode(), CloudRedisTemplateResultCode.CODE300001.getMsg());
        }
        return redisTemplate.getExpire(cloudRedisVO.getKey(), TimeUnit.SECONDS);
    }

    /**
     * 普通缓存获取
     * * @return 值
     */
    public Object get(CloudRedisVO cloudRedisVO) {
        if (null == cloudRedisVO.getKey()) {
            throw new CommonCustomException(CloudRedisTemplateResultCode.CODE300001.getCode(), CloudRedisTemplateResultCode.CODE300001.getMsg());
        }
        return cloudRedisVO.getKey() == null ? null : redisTemplate.opsForValue().get(cloudRedisVO.getKey());
    }

}
